<template>
  <el-container class="main-container">
    <!-- 头部区域 -->
    <el-header>
      <!-- 左侧的 logo -->
      <img src="@/assets/images/logo.png" alt="" />
      <!-- 右侧的菜单 -->
      <div class='userInfo'>
        <!-- 用户头像 -->
        <img :src="userInfo.user_pic" alt="" class="avatar" v-if="userInfo.user_pic"/>
        <img src="@/assets/logo.png" alt="" class="avatar" v-else/>
        <!-- 用户名称 -->
        <span>{{userInfo.nickname || userInfo.username}}</span>

        <span @click="clickLogout">退出</span>
      </div>
    </el-header>
    <el-container>
      <!-- 侧边栏区域 -->
      <el-aside width="200px">
         <div class="user-box">
          <img :src="userInfo.user_pic" alt="" v-if="userInfo.user_pic"/>
          <img src="@/assets/logo.png" v-else/>
          <span>欢迎 {{userInfo.nickname || userInfo.username}}</span>
        </div>

        <!-- 左侧菜单 -->
        <el-menu
          default-active="/home"
          class="el-menu-vertical-demo"
          background-color="#23262E"
          text-color="#fff"
          active-text-color="#f60"
          unique-opened
          router
        >
          <!-- 不包含子菜单的“一级菜单” -->
          <!--
              vue2中不能v-for与v-if混合使用
              v-for的优选级大于v-if
           -->
          <template v-for="item in menuList" >
            <el-menu-item :index="item.indexPath" :key="item.indexPath" v-if="!item.children">
              <i :class="item.icon"></i>{{item.title}}
            </el-menu-item>
          <!-- 包含子菜单的“一级菜单” -->
            <el-submenu :index="item.indexPath" :key="item.indexPath" v-else>
              <template slot="title">
                <i :class="item.icon"></i>
                <span>{{item.title}}</span>
              </template>

              <el-menu-item :index="subItem.indexPath" v-for="subItem in item.children" :key="subItem.indexPath">
                <i :class="subItem.icon"></i>{{subItem.title}}
              </el-menu-item>

            </el-submenu>
          </template>

        </el-menu>
      </el-aside>
      <el-container>
        <!-- 页面主体区域 -->
        <el-main>
          <!-- 二级路由 -->
          <router-view></router-view>
        </el-main>
        <!-- 底部 footer 区域 -->
        <el-footer>© 课工厂之家</el-footer>
      </el-container>
    </el-container>
  </el-container>
</template>

<script>
import { mapState } from 'vuex'
import { getMenus } from '@/api/menu'
export default {
  name: 'MainIndex',
  computed: {
    ...mapState(['userInfo', 'token'])
  },
  created () {
    if (this.token) {
      this.$store.dispatch('initUserInfo')
      this.initMenus()
    } else {
      this.$router.push('/login')
    }
  },
  data () {
    return {
      menuList: []
    }
  },
  methods: {
    // 退出
    async clickLogout () {
      const res = await this.$confirm('确定要退出吗', '提示').catch(error => error)
      if (res !== 'confirm') return

      // 清空vuex中的token
      this.$store.commit('setToken', '')
      // 跳转到登录页
      this.$router.push('/login')
    },

    // 获取菜单
    async initMenus () {
      const { data: res } = await getMenus()
      if (res.code !== 0) return
      this.menuList = res.data
    }
  }
}
</script>

<style lang="less" scoped>
.main-container {
  height: 100%;
  .el-header,
  .el-aside {
    background-color: #23262e;
    .user-box {
      height: 70px;
      display: flex;
      justify-content: center;
      align-items: center;
      border-top: 1px solid #000;
      border-bottom: 1px solid #000;
      user-select: none;
      img {
        width: 35px;
        height: 35px;
        border-radius: 50%;
        background-color: #fff;
        margin-right: 15px;
        object-fit: cover;
      }
      span {
        color: white;
        font-size: 12px;
      }
    }
    .el-submenu,
    .el-menu-item {
      width: 200px;
      user-select: none;
    }
  }
  .el-header {
    padding: 0;
    display: flex;
    justify-content: space-between;
    .userInfo {
      display: flex;
      align-items: center;
      span {
        margin:0 5px;
        color:white;
        font-size: 16px;
      }
    }
  }
  .el-main {
    overflow-y: scroll;
    height: 0;
    background-color: #F2F2F2;
  }
  .el-footer {
    background-color: #eee;
    font-size: 12px;
    display: flex;
    justify-content: center;
    align-items: center;
  }
}

.avatar {
  border-radius: 50%;
  width: 35px;
  height: 35px;
  background-color: #fff;
  margin-right: 10px;
  object-fit: cover;
}
</style>
